package com.wavehi.exce;

import com.wavehi.myannotation.MyAnnotation;
import com.wavehi.myannotation.MyAnnotations;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.Arrays;

/**
 * @Author WaveHi
 * @Date 30/8/2021 上午 9:37
 * @Version 1.0
 * @Contact HJBWaveHi@163.com
 * @Description Demonstrate the use of Annotation
 */
public class AnnotationDemo1 {

    public static void main(String[] args) {
        AnnotationDemo1 ad = new AnnotationDemo1();
//        ad.printData(1, 2, 23, 33);
        //测试注解的获取方式
        ad.printDatas();
    }

    @SafeVarargs
    public static <T> void printData(T... args) {
        System.out.println(Arrays.toString(args));
    }

    @SuppressWarnings(value = "unchecked")
    public <T> void printDataUp(T... datas) {

    }

    @MyAnnotation("Wow!")
    @MyAnnotation("Hello!")
    public <T> void printDatas(T... datas) {
        //获取字节码对象
        Class<AnnotationDemo1> annotationDemo1Class = AnnotationDemo1.class;
        //获取方法
        Method printDatas = null;
        try {
            printDatas = annotationDemo1Class.getMethod("printDatas", Object[].class);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
        //获取注解
        System.out.println(printDatas.isAnnotationPresent(MyAnnotations.class));
        Annotation[] annotations = annotationDemo1Class.getAnnotations();
        Annotation[] annotations1 = printDatas.getAnnotations();
        Annotation annotation = annotations1[0];
        //获取注解信息
        MyAnnotations annotation1 = (MyAnnotations) annotation;
        System.out.println(annotation);
        System.out.println(annotation1.value());
        MyAnnotation[] value = annotation1.value();
        for(MyAnnotation myAnnotation : value) {
            System.out.println(myAnnotation.value());
        }
    }

}
